package eu.danielhernandez.ejemplos.nombreceltibero.listener;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.hibernate.SessionFactory;

import eu.danielhernandez.ejemplos.nombreceltibero.utilities.HibernateUtil;

public class RenderResponsePhaseListener implements PhaseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 249648124244302375L;

	@Override
	public void afterPhase(PhaseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforePhase(PhaseEvent arg0) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		try{
			sessionFactory.getCurrentSession().getTransaction().commit();
		 } catch (Throwable ex) {
		      System.err.println("Very unsuitable error occured ...");
		 }
		 if(sessionFactory.getCurrentSession().getTransaction().isActive()){
			 sessionFactory.getCurrentSession().getTransaction().rollback();
		 }
	}

	@Override
	public PhaseId getPhaseId()  {
		return PhaseId.RENDER_RESPONSE;
	}

}
